package algorithms

//获取n项Fibonacci数的和
func Fibonacci(n int64) int64 {
	if n < 2 {
		return n
	}
	return Fibonacci(n-1) + Fibonacci(n-2)
}
func Fibonacci2(n int64) int64 {
	if n < 2 {
		return n
	}
	var i, fn, fnn, fnnn int64 = 2, 1, 1, 0
	for i = 2; i <= n; i++ {
		fn = fnn + fnnn
		fnnn = fnn
		fnn = fn
	}
	return fn
}

//获取小于n的所有Fibonaci数
func GetFibonacci(n int64) []int64 {
	f := []int64{0}
	if n < 2 {
		return f
	}
	var fn, fnn, fnnn int64 = 1, 1, 0
	for fn < n {

		f = append(f, fn)
		fn = fnn + fnnn
		fnnn = fnn
		fnn = fn
	}
	return f
}

//获取前n的Fibonaci数
func GetFibonacciN(n int64) []int64 {
	f := []int64{0}
	if n < 2 {
		return f
	}
	var fn, fnn, fnnn int64 = 1, 1, 0
	for int64(len(f)) < n {

		f = append(f, fn)
		fn = fnn + fnnn
		fnnn = fnn
		fnn = fn
	}
	return f
}
